package com.example.gameapplication.bullet;

import android.content.res.Resources;
import android.graphics.Canvas;
import com.example.gameapplication.database.DataUtils;

import java.util.ArrayList;
import java.util.List;

public class Skill {
    Resources resources;
    List<SkillBullet> skillBullets;

    public List<SkillBullet> getSkillBullets() {
        return skillBullets;
    }

    public void setSkillBullets(List<SkillBullet> skillBullets) {
        this.skillBullets = skillBullets;
    }

    private boolean isInit = false;
   private boolean isFinish = true;//大招是否结束，默认是true，就是结束状态

    public boolean isFinish() {
        return isFinish;
    }

    public void setFinish(boolean finish) {
        isFinish = finish;
    }

    public Skill(Resources resources) {
        this.resources = resources;
        init();
    }

    public void init() {
        if (skillBullets == null) {
            skillBullets = new ArrayList<>();
            DataUtils.getExecutorService().execute(initRunnable);
        }
    }

    Runnable initRunnable = new Runnable() {
        @Override
        public void run() {
            addSkillBuller();
        }
    };

    private void addSkillBuller() {
//        这个大招太慢了
//        for (int i = 0; i < 360; i++)
//            skillBullets.add(new SkillBullet(resources, DataUtils.getPaint(), enemyPlane, DataUtils.getTestPlane(resources), DataUtils.getSkillSpeed(),Math.PI*i/180));
//        for (int i = 360; i > 0; i--)
//            skillBullets.add(new SkillBullet(resources, DataUtils.getPaint(), enemyPlane, DataUtils.getTestPlane(resources), DataUtils.getSkillSpeed(), Math.PI*i/180));
//        for (int i = 0; i < 360; i++)
//            skillBullets.add(new SkillBullet(resources, DataUtils.getPaint(), enemyPlane, DataUtils.getTestPlane(resources), DataUtils.getSkillSpeed(), Math.PI*i/180));
        for (int i = 0; i < 180; i++)
            skillBullets.add(new SkillBullet(resources, DataUtils.getPaint(), DataUtils.getmainPlane(resources), DataUtils.getSkillSpeed(), i/7/ Math.PI));
        isInit = true;
    }


    Runnable reSetRunnable = new Runnable() {
        @Override
        public void run() {
            for (SkillBullet skillBullet : skillBullets) {
                skillBullet.setAlive(true);
                skillBullet.setLv(DataUtils.getmainPlane(resources).getLv());
            }
            isFinish=false;
        }
    };
    //重设大招,并且启动
    public void launchSkill(){
        if (isFinish)
        DataUtils.getExecutorService().execute(reSetRunnable);
    }
    int drawCont=0;

//    public void drawSelf(Canvas canvas) {
//        if (!isInit) return;//大招还没初始化
//        if (isFinish) return;//大招结束
//        if (drawCont<skillBullets.size()){
//            drawCont+=2;
//            if (drawCont>skillBullets.size()){
//                drawCont=skillBullets.size();
//            }
//        }
//        int sun = 0;
//        for (int i = 0; i <drawCont; i++) {
//            sun += skillBullets.get(i).drawSkillSelf(canvas);
//        }
//        if (sun == 90) {//全部子弹死亡，不在画大招
//            isFinish = true;
//            drawCont=0;
//        }
//    }
    public void drawSelf(Canvas canvas) {
        if (!isInit) return;//大招还没初始化
        if (isFinish) return;//大招结束

        int sun = 0;
        for (int i = 0; i <skillBullets.size(); i++) {
            sun += skillBullets.get(i).drawSkillSelf(canvas);
        }
        if (sun == 180) {//全部子弹死亡，不在画大招
            isFinish = true;
            drawCont=0;
        }
        DataUtils.runSkillAttack();//因为性能问题，不把他放到run里边运算，毕竟大招不可能一直在使用状态
    }

    public void dataDestroyed(){
        if (skillBullets!=null)
        while (skillBullets.size()>0){
            skillBullets.get(0).dataDestroyed();
            skillBullets.remove(0);
        }
        skillBullets=null;
        isInit=false;
    }
}
